package com.ld.rightsSystem.utils;

import com.ld.rightsSystem.pojo.TPermission;
import com.ld.rightsSystem.vo.TPermissionTree;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.security.Permission;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Utils <T>{

    public static String passwordEncrypt(String password) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.encode(password);
    }

    public static String getNowTime(String patternNew){
        String pattern = "";
        if(patternNew != null & patternNew.length() >0){
            pattern = patternNew;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(new Date());
    }

    public static List<TPermissionTree> getPermissionList(List<TPermission> permissions) {
        List<TPermissionTree> treeList = new ArrayList<>();
        for (TPermission node : permissions) {
            if (node.getParentId() == null) {
                TPermissionTree tPermissionTree = new TPermissionTree(node);
                List<TPermission> children = new ArrayList<>();
                permissions.forEach(item->{
                    if(item.getParentId() != null && item.getParentId().equals(node.getPermissionId())){
                        children.add(item);
                    }
                });
                tPermissionTree.setChildren(children);
                treeList.add(tPermissionTree);
            }
        }
        return treeList;
    }
}
